package me.imli.app.ui.fragment;

import me.imli.app.R;
import me.imli.app.util.ThemeUtil;
import me.imli.app.util.ToastUtil;
import me.imli.app.volley.RequestListener;
import me.imli.app.volley.RequestManager;
import me.imli.app.volley.RequestParams;
import me.imli.app.widget.crouton.Style;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;

public class BaseFragment extends Fragment {

	/** TAG */
	protected final String TAG = getClass().getName();
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	/**
	 * 打开一个新的Activity
	 * @param clazz
	 */
	protected void openActivity(Class<?> clazz) {
		try {
			Intent intent = new Intent(getActivity(), clazz);
			startActivity(intent);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 打开一个新的Activity
	 * @param clazz
	 * @param requestCode
	 */
	protected void openActivity(Class<?> clazz, int requestCode) {
		try {
			Intent intent = new Intent(getActivity(), clazz);
			startActivityForResult(intent, requestCode);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 显示 Toast
	 * @param resid
	 */
	protected void showToast(int resid) {
		ToastUtil.showShort(getActivity(), resid, getToastStyle(), R.id.ll_toast);
	}
	
	/**
	 * 显示 Toast
	 * @param msg
	 */
	protected void showToast(String msg) {
		ToastUtil.showShort(getActivity(), msg, getToastStyle(), R.id.ll_toast);
	}
	
	/**
	 * 获取 Toast 样式
	 * @return
	 */
	protected Style getToastStyle() {
		switch (ThemeUtil.getCurTheme()) {
		case ThemeUtil.THEME_BULE:
			return Style.BLUE;
		case ThemeUtil.THEME_NIGHT:
			return Style.NIGHT;
		default:
			return Style.BLUE;
		}
	}
	
	/**
	 * 
	 * @param url
	 * @param l
	 */
	protected void get(String url, RequestListener l) {
		get(url, null, l);
	}
	
	/**
	 * 
	 * @param url
	 * @param params
	 * @param l
	 */
	protected void get(String url, RequestParams params, RequestListener l) {
		RequestManager.get(url, this, params, l);
	}
	
	/**
	 * 
	 * @param url
	 * @param l
	 */
	protected void post(String url, RequestListener l) {
		post(url, null, l);
	}
	
	/**
	 * 
	 * @param url
	 * @param params
	 * @param l
	 */
	protected void post(String url, RequestParams params, RequestListener l) {
		RequestManager.post(url, this, params, l);
	}
	
	@Override
	public void onDestroy() {
		RequestManager.cancelAll(this);
		super.onDestroy();
	}
}
